<?php

/**
  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; version 3 of the License.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.
 */

/**
 * Autoloader class with spl_autoload_register function
 * @author pilou@pilou.hu
 * @version 0.2
 * @return void
 */
class Autoloader {

	/**
	 * Contructor of the default Autoloader class
	 * @access public
	 * @return void
	 */
	public function __construct() {
		spl_autoload_register(array($this, "_autoload"));
	}
	
	/**
	 * Autoloads Core classes (system), if not found loads libraries classes
	 * @access private
	 * @param string $className Class name to be loaded
	 * @return void
	 */
	private function _autoload($className) {
		$className = str_replace("\\", DIRECTORY_SEPARATOR, ltrim($className, "\\"));
		if (!$this->_autoloadSystemClasses($className)){
			$this->_autoloadClasses($className);
		}
	}

	/**
	 * Autoloads Core classes (system)
	 * @access private
	 * @param string $className Core class name to be loaded
	 * @return bool
	 */
	private function _autoloadSystemClasses($className) {
		if (strpos($className, "Core") === 0) {
			@include_once(DIR_CORE . DIRECTORY_SEPARATOR . substr($className, 5) . ".php");
			return true;
		}
		return false;
	}
	
	/**
	 * Autoloads libraries classes
	 * @access private
	 * @param string $className libraries class name to be loaded
	 * @return void
	 */
	private function _autoloadClasses($className) {
		@include_once(DIR_LIB . DIRECTORY_SEPARATOR . $className . ".php");
	}

}

new Autoloader();

?>